package com.szd.qiongyou20.home.search.result_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.szd.qiongyou20.R;
import com.szd.qiongyou20.home.search.adapter.ViewPager_Search_Adapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by SZD on 2016/12/27.
 */

public class Result_Fragment extends Fragment {

    private Context mContext;
    private FragmentManager mFragmentManager;
    private List<Fragment> mList;
    private String[] TabTitles;
    private String mKeyword;

    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    public static Result_Fragment newInstance(String keyword) {
        Bundle bundle = new Bundle();
        Result_Fragment fragment = new Result_Fragment();
        bundle.putString("keyword", keyword);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mList = new ArrayList<>();
        mFragmentManager = getFragmentManager();
        TabTitles = getResources().getStringArray(R.array.search_result);
        mKeyword = getArguments().getString("keyword");
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.search_result_layout, null);
        mTabLayout = (TabLayout) view.findViewById(R.id.tabLayout_search);
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager_search);
        mViewPager.setOffscreenPageLimit(6);

        fragmentList();

        ViewPager_Search_Adapter viewPager_search_adapter =
                new ViewPager_Search_Adapter(mFragmentManager, mList, TabTitles);
        mViewPager.setAdapter(viewPager_search_adapter);
        mTabLayout.setupWithViewPager(mViewPager);
        return view;
    }

    private void fragmentList() {
        // 目的地
        Mdd_Fragment mdd_fragment = Mdd_Fragment.newInstance(mKeyword);
        mList.add(mdd_fragment);
        // 锦囊
        Jn_Fragment jn_fragment = Jn_Fragment.newInstance(mKeyword);
        mList.add(jn_fragment);
        // 问答
        Ask_Fragment ask_fragment =  Ask_Fragment.newInstance(mKeyword);
        mList.add(ask_fragment);
        // 帖子
        TieZi_Fragment tieZi_fragment = TieZi_Fragment.newInstance(mKeyword);
        mList.add(tieZi_fragment);
        // 商品
        Goods_Fragment goods_fragment = Goods_Fragment.newInstance(mKeyword);
        mList.add(goods_fragment);
        // 用户
        User_Fragment user_fragment = User_Fragment.newInstance(mKeyword);
        mList.add(user_fragment);
    }


}
